<template>
  <div class="building">
    <!-- 位置显示 -->
    <div class='position'>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/building' }">楼宇管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 1. 搜索区域 -->

    <div class="header">
      <!-- change	仅在输入框失去焦点或用户按下回车时触发 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="楼宇名称">
          <el-input v-model="formInline.buildingName" placeholder="楼宇名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="FindByName()">查询</el-button>
          <el-button type="success" icon="el-icon-circle-plus-outline" @click="openAddDialog()">添加</el-button>
          <template>
            <download-excel style="padding: 0;margin: 0;float: right;" class="export-excel-wrapper" :data="tableData" name="building.xls">
              <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
              <el-button type="primary" icon="el-icon-download">导出数据</el-button>
            </download-excel>
          </template>
        </el-form-item>
      </el-form>
    </div>

    <!-- 2. 表格区域展示视图数据 -->
    <div class="wrapper">
      <el-table :data="tableData" border>
        <el-table-column prop="id" label="ID" sortable width="60"></el-table-column>
        <el-table-column prop="name" label="楼宇名称" width="120"></el-table-column>
        <el-table-column prop="location" label="楼宇定位" width="120"></el-table-column>
        <el-table-column prop="dormitoryManager" label="楼宇管理员">
          <template slot-scope="scope">
            <span v-if="scope.row.dormitoryManager!=null">{{scope.row.dormitoryManager.name}}</span>
            <span v-else style="color:red;">暂无未添加宿管</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间"></el-table-column>
        <el-table-column prop="version" label="修改次数" width="80"></el-table-column>
        <el-table-column prop="status" label="状态码" width="70">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.status" effect="dark">正常</el-tag>
            <el-tag type="info" effect="dark" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="350">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="findDate(scope.row)">查看</el-button>
            <el-button type="success" size="mini" v-if="role == 2" @click="changeManager(scope.row)">
              成为楼宇管理</el-button>
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit">
              编辑</el-button>
            <el-button size="mini" type="danger" v-if="role == 1" @click="handleDelete(scope.row)" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 3. 分页 -->
    <MyPagination :total="total" :pageSize="pageSize" @SizeChange="SizeChange" @Pagechange="Pagechange" :currentPage="currentPage" />
    <!-- 4.查看数据 -->
    <el-dialog title="查看楼宇信息" :visible.sync="MessageDalog" width="70%" :before-close="handleClose">
      <el-table :data="rowData">
        <el-table-column prop="id" label="ID" sortable width="60"></el-table-column>
        <el-table-column prop="name" label="楼宇名称" width="120"></el-table-column>
        <el-table-column prop="location" label="楼宇定位" width="120"></el-table-column>
        <el-table-column prop="dormitoryManager" label="楼宇管理员">
          <template slot-scope="scope">
            <span v-if="scope.row.dormitoryManager!=null">{{scope.row.dormitoryManager.name}}</span>
            <span>暂无未添加宿管</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间"></el-table-column>
        <el-table-column prop="version" label="修改次数" width="80"></el-table-column>
        <el-table-column prop="status" label="状态码" width="70">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.status" effect="dark">正常</el-tag>
            <el-tag type="info" effect="dark" v-else>停用</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 5.添加 -->
    <el-dialog title="添加楼宇" :visible.sync="Dialog">
      <el-form :model="Dialogfrom">
        <el-form-item label="楼宇名称" label-width="120px">
          <el-input v-model.trim="Dialogfrom.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="楼宇定位" label-width="120px">
          <el-input v-model.trim="Dialogfrom.location" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddBuilding()">取 消</el-button>
        <el-button type="primary" @click="submitAddFrom()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改dialog -->
    <el-dialog title="修改楼宇信息" :visible.sync="updateDialog">
      <el-form :model="updatefrom">
        <el-form-item label="楼宇名称" label-width="120px">
          <el-input v-model.trim="updatefrom.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="楼宇定位" label-width="120px">
          <el-input v-model.trim="updatefrom.location" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialog =false ">取 消</el-button>
        <el-button type="primary" @click="submitupdFrom()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import MyPagination from "../components/MyPagination.vue"
import { GetPageBuilding, addBuilding, updateBuilding, deleteBuilding, getchangeManager } from '@/api/api'
export default {
  components: {
    MyPagination
  },
  computed: {
    ...mapState(['role', 'userinfo']),
  },
  data() {
    return {
      //搜索form
      formInline: {
        buildingName: '',
      },
      tableData: [],//展示页面数据
      total: 5,//总数
      currentPage: 1, //选中的高亮页码
      pageSize: 5,//一页几条数据
      dialogVisible: false,
      // 查看
      MessageDalog: false,
      rowData: [], //当前行的数据对象
      // 增加
      Dialog: false,
      Dialogfrom: {
        name: '',
        location: ''
      },
      title: '',
      // 修改
      updateDialog: false,
      oldData: {},//保存修改之前的数据
      updatefrom: {
        name: '', location: ''
      }
    };
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      GetPageBuilding({ pageSize: this.pageSize, pageNum: this.currentPage, name: this.formInline.buildingName }).then((result) => {
        console.log(result);
        if (result.status == 200) {
          this.tableData = result.data.list
          this.total = result.data.total
          this.pageSize = result.data.pageSize
          this.currentPage = result.data.pageNum
        } else {
          this.$message.error(result.msg)
        }
      })
    },
    FindByName() {
      if (this.formInline.buildingName == '') return this.$message.error('请输入完整的信息之后在查询！')
      GetPageBuilding({ pageSize: this.pageSize, pageNum: this.currentPage, name: this.formInline.buildingName }).then((result) => {
        console.log(result);
        if (result.status == 200) {
          this.$message.success(result.msg)
          this.tableData = result.data.list
          this.total = result.data.total
          this.pageSize = result.data.pageSize
          this.currentPage = result.data.pageNum
        } else {
          this.$message.error(result.msg)
        }
      })
      this.formInline.buildingName = ''
      this.formInline.dormitoryManagerAccount
    },
    Pagechange(pageNum) {//改变页码
      console.log(pageNum);
      this.currentPage = pageNum
      this.loadData()
    },
    SizeChange(pageSize) {//改变一页有几条
      console.log(pageSize);
      this.pageSize = pageSize
      this.loadData()
    },
    findDate(row) {//打开每行数据dialog
      console.log(row);
      this.rowData.push(row)//添加一行数据到rowdata变量
      this.MessageDalog = true//显示对话框
    },
    handleClose() {//关闭对话框
      this.MessageDalog = false
      this.rowData = []//清空数据
    },
    openAddDialog() {//打开新增dialog
      this.Dialog = true
    },
    cancelAddBuilding() {//关闭dialog 并且清空输入框
      // this.$message.info('取消新增')
      this.Dialog = false
      this.Dialogfrom.name = ''
      this.Dialogfrom.location = ''
    },
    submitAddFrom() {
      if (this.Dialogfrom.name == '' || this.Dialogfrom.location == '') return this.$message.error('请输入完整的信息')
      addBuilding(this.Dialogfrom).then(res => {
        console.log(res);
        if (res.status == 200) {
          this.$message.success(res.msg)
          this.updateDialog = false
          this.loadData()//重新加载
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleEdit(data) {
      this.updateDialog = true
      this.updatefrom.name = data.name
      this.updatefrom.location = data.location
      this.oldData = data
    },
    submitupdFrom() {
      if (this.updatefrom.name === this.oldData.name && this.updatefrom.location === this.oldData.location) return this.$message.error('与原来相同你修改个锤子')
      let payload = { id: this.oldData.id, name: this.updatefrom.name, location: this.updatefrom.location }
      console.log(payload);
      updateBuilding(payload).then(res => {
        console.log(res);
        if (res.status == 200) {
          this.$message.success(res.msg)
          this.updateDialog = false
          this.loadData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleDelete(data) {//删除
      this.$confirm('确认删除该楼宇吗?')
        .then(_ => {
          deleteBuilding({ id: data.id }).then(res => {
            console.log(res);
            if (res.status == 200) {
              this.$message.success(res.msg)
              this.loadData()
            } else {
              this.$message.error(res.msg)
            }
          })
        })
        .catch(_ => {
          this.$message.info('取消删除')
        });
    },
    changeManager(data) {
      if (this.role != 2) return this.$message.error("只有宿管才能成为楼宇管理员哦!")
      this.$confirm('确认成为该楼宇管理员吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log(data.id);
          console.log(this.userinfo.id);
          getchangeManager({mid:this.userinfo.id,id:data.id}).then(res =>{
            console.log(res);
            if (res.status ===200 ) {
              this.$message.success(res.msg)
              this.loadData()
            }else{
              this.$message.error(res.msg)
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  }
}
</script>

<style lang='less' scoped>
.position {
  // background: #fff;
  padding: 10px;
  // border: 1px solid #eee;
  margin-bottom: 10px;
}
.header {
  display: flex;
  background: #fff;
  padding: 10px;
  border: 1px solid #eee;
  button {
    margin-left: 20px;
  }
  .el-form-item {
    margin-bottom: 0;
  }
}
.wrapper {
  margin: 10px 0;
}
</style>